package com.fr.base.core;

import com.fr.base.ArrayUtils;
import com.fr.util.Consts;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.Date;

public class RSADecode {
	private static final BigInteger N = new BigInteger(
			"61103299352066102812915201580370346997919089893149305765565972348630053713717591736527153881172892494135635969333391530396986735629281282430026953431657619628355730192943385620088393498664105803897708601718035436482482749378713844253725606147581454234307387984660050507963063894825237808748868429675256901161");
	private static final BigInteger D = new BigInteger("65537");

	public static void decode(InputStream paramInputStream,
			OutputStream paramOutputStream) {
		try {
			byte[] arrayOfByte = new byte[paramInputStream.available()];
			paramInputStream.read(arrayOfByte);
			decode(arrayOfByte, paramOutputStream);
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	public static void decode(byte[] paramArrayOfByte,
			OutputStream paramOutputStream) {
		if ((paramArrayOfByte != null) && (paramArrayOfByte.length == 0)) {
			try {
				// 机器的mac地址
				String macaddress = BaseCoreUtils.getMacAddress();
				// fr版本号
				String version = Consts.VERSION; // 不同fr版本的jar包中，该变量名称不一样，视具体情况做修改
				// 使用期限
				String deadline = (new Date().getTime() * 2) + "";
				/**
				 * 4表示基本版， 3表示开发版， 2表示标准版， Standard表示专业版， Enterprise表示企业版
				 */
				String edition = "Enterprise";
				// 最大并发数
				String concurrency = "1000";
				String json = "{'macaddress':'" + macaddress + "', 'version':'"
						+ version + "', 'deadline':'" + deadline
						+ "', 'edition':'" + edition + "', 'concurrency':'"
						+ concurrency + "'}";
				paramOutputStream.write(json.getBytes("UTF-8"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			decode(byte2hex(paramArrayOfByte), paramOutputStream);
		}
	}

	public static void decode(String paramString, OutputStream paramOutputStream) {
		String[] arrayOfString = paramString.split(byte2hex(N.toByteArray()));
		try {
			for (int i = 0; i < arrayOfString.length; ++i)
				paramOutputStream.write(tinyDecode(hex2byte(arrayOfString[i])));
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
		}
	}

	private static String byte2hex(byte[] paramArrayOfByte) {
		StringBuffer localStringBuffer = new StringBuffer();
		String str = "";
		for (int i = 0; i < paramArrayOfByte.length; ++i) {
			str = Integer.toHexString(paramArrayOfByte[i] & 0xFF);
			if (str.length() == 1)
				localStringBuffer.append("0");
			localStringBuffer.append(str);
		}
		return localStringBuffer.toString().toUpperCase();
	}

	private static byte[] hex2byte(String paramString) {
		if (paramString == null)
			return null;
		int i = paramString.length();
		if (i % 2 == 1)
			return null;
		byte[] arrayOfByte = new byte[i / 2];
		for (int j = 0; j != i / 2; ++j)
			arrayOfByte[j] = (byte) Integer.parseInt(paramString.substring(
					j * 2, j * 2 + 2), 16);
		return arrayOfByte;
	}

	private static byte[] tinyDecode(byte[] paramArrayOfByte) {
		paramArrayOfByte = new BigInteger(paramArrayOfByte).modPow(D, N)
				.toByteArray();
		return ArrayUtils
				.subarray(paramArrayOfByte, 1, paramArrayOfByte.length);
	}
}